﻿package com.cilico.single.components;

import java.util.Vector;

import android.graphics.Bitmap;

import com.cilico.single.Mid;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;

public class POI {
	String name_;
	public String desc_;
	String tag_;
	public String phone_;
	String updated_; // time in ISO8601 format (used in XML schemas)
	public String showEntryUrl_;// dual feed content url
	public int showEntryLen_; // the numbers of content item.
	private Vector<Long> occurrence_; // Event times at this POI
	public boolean isInterrupteLoad_;
	public boolean popStack_;//一定要弹栈
	public boolean skip_; //标记是否继续弹栈
	public boolean skip_1;
	public boolean skip_2;
	public String skip_number;
	/** String-based ad; to be displayed in POI detail view. */
	public String adString_;
	public String adUrl_;
	/** 15 minutes after this time, we should reload the ad. */
	public long adTime_;

	/** Unique internal ID */
	int id_;

	/** Link to the original content of this POI. */
	public String url_;

	/**
	 * A list of ChannelParams to keep track of per-entry variables. These values are used in ChannelAction for per-POI
	 * RPC calls.
	 */
	public Vector<ChannelParam> entryVariables_;

	/**
	 * We allow display multiple photos in a POI object.
	 */
	public Vector<Bitmap> images_;
	public Vector<String> imgUrls_;
	public Vector<String> imgLinks_;
	public int totalImages_;
	public int numImgLine_;
	/**
	 * A list of extended attribute names and values
	 */
	public Vector<String> eattrNames_;
	public Vector<String> eattrValues_;

	public POI() {
		super();
		occurrence_ = new Vector<Long>();
		entryVariables_ = new Vector<ChannelParam>();
		eattrNames_ = new Vector<String>();
		eattrValues_ = new Vector<String>();
		isInterrupteLoad_ = false;
		adTime_ = System.currentTimeMillis();

		images_ = new Vector<Bitmap>();
		imgUrls_ = new Vector<String>();
		imgLinks_ = new Vector<String>();
		totalImages_ = numImgLine_ = 0;
	}


	public void setName(String name) {
		if (name != null) {
			int idx = name.indexOf("Review Of");
			if (idx >= 0) {
				name_ = name.substring(idx + 10);
			} else {
				name_ = name;
			}
		}
	}

	void setUpdated(String text) {
		if (text.length() > 0) {
			try {
				updated_ = text;
			} catch (Exception ex) {
				Utils.printOutToConsole("date = " + text + "，POI-setUpdated:" + ex.toString());
				updated_ = null;
			}
		}
	}

	/**
	 * NOTE: field must be a string that is used in the POI sorting choice screen. XXX FIXME: this should be implemented
	 * as a hash table lookup based on tag names in this POI.
	 */
	public int compareBy(POI p, String field) {
		if (field.equals("Phone")) {
			if (phone_ == null)
				return (p.phone_ == null) ? 0 : 1;
			else
				return (p.phone_ == null) ? -1 : phone_.compareTo(p.phone_);
		} else if (field.equals("Name")) {
			if (name_ == null)
				return (p.name_ == null) ? 0 : 1;
			else
				return (p.name_ == null) ? -1 : name_.toUpperCase().compareTo(p.name_.toUpperCase());
		} else if (field.equals("Time")) {
			if (updated_ == null)
				return (p.updated_ == null) ? 0 : 1;
			else
				// Descending order of time.
				return (p.updated_ == null) ? -1 : -updated_.compareTo(p.updated_);
		} else {
			// By default, sort by Distance.
			return Integer.MAX_VALUE;
		}
	}

	public String name() {
		return name_;
	}

	/**
	 * Add an extended attribute. Special case: if the name is 'Phone', we will display it as a dialable phone number.
	 */
	void addExtAttr(String name, String value) {
		if (name.equals("Phone")) {
			phone_ = value;
		} else {
			eattrNames_.addElement(name);
			eattrValues_.addElement(value);
		}
	}

	void addOccurrence(long t) {
		// Make sure that dates in occurrence_ are in ascending order!
		int i = 0;
		for (; i < occurrence_.size(); ++i) {
			Long v = (Long) occurrence_.elementAt(i);
			if (v.longValue() > t) {
				occurrence_.insertElementAt(new Long(t), i);
				break;
			}
		}
		if (i >= occurrence_.size())
			occurrence_.addElement(new Long(t));
		StringBuffer str = new StringBuffer();
		Utils.formatTimeString(t, str);
		Utils.printOutToConsole("added occurrence " + t + " time " + str.toString());
	}

	/**
	 * Add time when this POI happened. We pass in time by epoch seconds. Note that internal time are *milliseconds*
	 * epoch time.
	 */
	void addOccurrence(String text) {
		try {
			long t = Long.parseLong(text) * 1000;
			addOccurrence(t);
		} catch (Exception ex) {
			Utils.printOutToConsole("POI-addOccurrence:" + ex.toString());
		}
	}

	public void checkUpdatesFromServer(final Mid mid, final BaseView view) {
		final boolean loadImg = (totalImages_ > 0 && (imgUrls_.size() > images_.size()));
		if (loadImg) {
			WaitDialog dlg = mid.waitDialog_;
			dlg.addBgTask(new WaitDialog.Task(2) {
				public void run(WaitDialog dlg) {
					if (loadImg) {
						CalculateImgSize();
						for (int i = 0; i < totalImages_; i++) {
							try {
								String imgUrl = (String) imgUrls_.elementAt(i);
								mid.downloadImage(imgUrl, i, view.preferredImageWidth(), view.preferredImageHeight(),
										images_);
							} catch (Exception ex) {
								Utils.printOutToConsole("POI-checkUpdatesFromServer-ex:" + ex.toString());
							}
						}
					}
				}

				public void onSuccess(WaitDialog dlg) {
					if (!isInterrupteLoad_) {
						// if (view instanceof DetailView) {
						// ((DetailView) view).adMobile_.setText(adString_);
						// }
						// view.refresh();重绘界面*********************************
					}
				}
			});
		}
	}

	public void CalculateImgSize() {
		// if we have at least 128 pixel width, show two photos side by side
		// (requesting each photo as 64x64), if we have >192 pixels, show 3
		// photos side by side.
		if (totalImages_ > 2 && Utils.screenWidth_ > 192) {
			numImgLine_ = 3;
		} else if (totalImages_ > 1 && Utils.screenWidth_ > 128) {
			numImgLine_ = 2;
		} else {
			numImgLine_ = 1;
		}
	}
}
